#include <stdio.h>
#include <SDL/SDL.h>

int main(int argc, char *argv[]) {
    SDL_Event event;
    SDL_Surface *super;
	SDL_Surface *screen;
	SDL_Surface *image;
	SDL_Surface *temp;
	SDL_Rect src, dest, R,D;
	int done = 1;
	if (SDL_Init(SDL_INIT_VIDEO) != 0) {
		printf("Unable to initialize SDL: %s\n", SDL_GetError());
		return 1;
	}

	screen = SDL_SetVideoMode(798, 601, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
	if (screen == NULL) {
		printf("Unable to set video mode: %s\n", SDL_GetError());
		return 1;
	}

	temp = SDL_LoadBMP("img/mario.bmp");
	if (temp == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
    image = SDL_DisplayFormat(temp);


    temp = SDL_LoadBMP("img/super.bmp");
	if (temp == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
    super= SDL_DisplayFormat(temp);

	src.x = 0;
	src.y = 0;
	src.w = image->w;
	src.h = image->h;
	dest.x = 0;
	dest.y = 0;
	dest.w = image->w;
	dest.h = image->h;

	R.x = 0;
	R.y = 0;
	R.w = super->w;
	R.h = super->h;
	D.x = 0;
	D.y = 0;
	D.w = super->w;
	D.h = super->h;

while(done)
{
	SDL_BlitSurface(image, &src, screen, &dest);
	SDL_BlitSurface(super, &R, screen, &D);

	SDL_Flip(screen);
	while (SDL_PollEvent(&event))
{

	switch (event.type)
	{

		case SDL_QUIT:
		done = 1;
		break;

		case SDL_KEYDOWN:
		{
		    switch(event.key.keysym.sym)
                     {
                      case SDLK_UP:
                       D.y=D.y-50;
                       temp = SDL_LoadBMP("img/up.bmp");
                   super = SDL_DisplayFormat(temp);

                   R.x = 0;
                   R.y = 0;
	               R.w = super->w;
	               R.h = super->h;

                       break;

                      case SDLK_DOWN:
                       D.y=D.y+50;
                       temp = SDL_LoadBMP("img/down.bmp");

                   super = SDL_DisplayFormat(temp);


	               R.w = super->w;
	               R.h = super->h;
                       break;

                      case SDLK_RIGHT:
                       D.x=D.x+50;
                       temp = SDL_LoadBMP("img/right.bmp");

                   super= SDL_DisplayFormat(temp);

	               R.w = super->w;
	               R.h = super->h;
                       break;

                      case SDLK_LEFT:
                       D.x=D.x-50;
                       temp = SDL_LoadBMP("img/left.bmp");

                   super = SDL_DisplayFormat(temp);


	               R.w = super->w;
	               R.h = super->h;
                       break;

                      case SDLK_RETURN:
                       done = 0;
                       break;

                      default :
                       done = 0;
                       break;
}
			if (event.key.keysym.sym == SDLK_ESCAPE)
			done = 1;
			break;
		}
	}
}

}
SDL_FreeSurface(temp);

	return 0;
}
